package com.lyy.mybatis.sensitive.utils;

import lombok.experimental.UtilityClass;

/**
 * @author lyy
 * @date 2024/11/30 17:36
 */
@UtilityClass
public class MaskUtil {

    /**
     * 用星号遮蔽指定字符串部分字符
     *
     * @param str   要遮蔽的字符串
     * @param start 开始的明文展示个数
     * @param end   结束的明文展示个数
     * @return 遮蔽后的字符串
     */
    public String maskWithStar(String str, int start, int end) {
        if (str == null) {
            return null;
        }
        if (str.isEmpty()) {
            return str;
        }
        int len = str.length();
        int maskLen = len - start - end;
        if (maskLen <= 0) {
            return str;
        }
        StringBuilder sb = new StringBuilder(len);
        sb.append(str, 0, start);
        for (int i = 0; i < maskLen; i++) {
            sb.append("*");
        }
        sb.append(str, start + maskLen, len);
        return sb.toString();
    }

}
